<!-- 唐礼飞  2020-10-26 09:36:37  -->
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<div class="modal fade" id="changePsswordModal" data-backdrop="static"
	tabindex="-1" role="dialog" aria-labelledby="staticBackdropLabel"
	aria-hidden="true">
	<div class="modal-dialog" role="document">
		<div class="modal-content">
			<div class="modal-header">
				<h5 class="modal-title" id="staticBackdropLabel">修改密码</h5>
				<button type="button" class="close" data-dismiss="modal"
					aria-label="Close">
					<span aria-hidden="true">&times;</span>
				</button>
			</div>
			<div class="modal-body">
				<form>
					<input type="hidden" id="id" value="${sessionScope.user.id}">
					<div class="form-group row">
						<label for="staticEmail" class="col-sm-3 col-form-label">用户名：</label>
						<div class="col-sm-9">
							<input type="text" readonly class="form-control-plaintext"
								value="${sessionScope.user.name}">
						</div>
					</div>
					<div class="form-group row">
						<label for="oldPassword" class="col-sm-3 col-form-label">原密码：</label>
						<div class="col-sm-9">
							<input type="password" class="form-control" id="oldPassword"
								required aria-describedby="hbOldPassword"> <small
								id="hbOldPassword" class="form-text text-danger"></small>
						</div>
					</div>
					<div class="form-group row">
						<label for="newPassword" class="col-sm-3 col-form-label">新密码：</label>
						<div class="col-sm-9">
							<input type="password" class="form-control" id="newPassword"
								required aria-describedby="hbNewPassword"> <small
								id="hbNewPassword" class="form-text text-danger"></small>
						</div>
					</div>
					<div class="form-group row">
						<label for="confirmPassword" class="col-sm-3 col-form-label">确认密码：</label>
						<div class="col-sm-9">
							<input type="password" class="form-control" id="confirmPassword"
								required aria-describedby="hbConfirmPassword"> <small
								id="hbConfirmPassword" class="form-text text-danger"></small>
						</div>
					</div>
				</form>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-primary"
					onclick="changePassword()">保存</button>
				<button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
			</div>
		</div>
	</div>
</div>
<script>
	function changePassword() {
		//判断数据是否输入
		checked = true;
		oldPwd = $("#oldPassword").val();
		if (oldPwd == "") {
			$("#hbOldPassword").text("请输入原密码！");
			checked = false;
		} else {
			$("#hbOldPassword").text("");
		}
		newPwd = $("#newPassword").val();
		if (newPwd == "") {
			$("#hbNewPassword").text("请输入新密码！");
			checked = false;
		} else {
			$("#hbNewPassword").text("");
		}
		confirmPwd = $("#confirmPassword").val();
		if (confirmPwd == "") {
			$("#hbConfirmPassword").text("请输入确认密码！");
			checked = false;
		} else {
			$("#hbConfirmPassword").text("");
		}
		if (!checked) {
			return;
		}
		//发送请求到服务器端进行密码修改
		$.ajax({
			url : "${pageContext.request.contextPath}/user/change/password",
			type : "POST",
			data : {
				id : $("#id").val(),
				oldPassword : oldPwd,
				newPassword : newPwd,
				confirmPassword : confirmPwd,
			},
			dataType : "json",
			success : function(result) {
				if (result.code === 0) {
					alert(result.message);
					$("#changePsswordModal").modal("hide");
				} else {
					alert(result.message);
				}
				$("#oldPassword").val("");
				$("#newPassword").val("");
				$("#confirmPassword").val("");
			},
			error : function(err) {
				alert("用户修改密码失败！");
				console.log(err);
			}
		});

	}
</script>